package com.health.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.dao.OrderMapper;
import com.health.pojo.CheckGroup;
import com.health.pojo.Order;
import com.health.vo.PageResult;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    /**
     * 查询是否重复预约
     */
    public int findCountByOrder(Order order) {
        return orderMapper.findCountByOrder(order);
    }

    /**
     * 添加预约
     */
    @Transactional(rollbackFor = Exception.class)
    public int add(Order order) {
        //添加预约
        orderMapper.add(order);
        //已预约人数加1-根据日期
        int count = orderMapper.updateReservations(order.getOrderDate());
        if (count == 0) {
            throw new RuntimeException("预约失败,请预约其他日期");
        }

        return order.getId();
    }

    /**
     * 预约到诊数据报告
     */
    public Map<String, Object> findOrderReport() {
        return orderMapper.findOrderReport();
    }

    /**
     * 订单列表分页查询
     */
    public PageResult<Order> findPage(Integer pageNow, Integer pageSize, String query) {
        //引入分页插件
        PageHelper.startPage(pageNow, pageSize);
        Page<Order> page = orderMapper.findByQuery(query);
        return new PageResult<Order>(page.getTotal(), page.getResult());
    }

    /**
     * 删除订单
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteById(Integer id) {
        //查询该订单
        Order order = orderMapper.findById(id);
        //获取订单日期
        Date orderDate = order.getOrderDate();
        //根据日期查询orderSetting表预订人数
        int count = orderMapper.findReservationByDate(orderDate);

        if (count <= 0) {
            //抛出异常
            throw new RuntimeException("预约设置尚无有人预约");
        }

        //orderSetting中reservations减1
        orderMapper.minusReservations(orderDate);
        //删除订单
        return orderMapper.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public int addOrder(Order order) {
        //添加预约
        orderMapper.add(order);
        //已预约人数加1-根据日期
        int count = orderMapper.updateReservations(order.getOrderDate());
        if (count == 0) {
            throw new RuntimeException("预约失败,请预约其他日期");
        }

        return count;
    }

    public Order findById(Integer id) {
        return orderMapper.findById(id);
    }

    /**
     * 修改预约
     * 注意:根据实际情况,修改预约时,需要先根据id查询预约
     */
    @Transactional(rollbackFor = Exception.class)
    public int update(Order order,Order order_old) {
        //修改预约
        int count=orderMapper.update(order);

        if (count>0){
            //是否修改日期
            if (!order.getOrderDate().equals(order_old.getOrderDate())){
                //日期不同,原来的ordersetting表预约人数减1
                //根据业务逻辑,原有预约人数不能小于1
                orderMapper.minusReservations(order_old.getOrderDate());
                //新添加的ordersetting表预约人数加1
               int count_new=orderMapper.updateReservations(order.getOrderDate());
               if (count_new==0){
                   throw new RuntimeException("预约失败,请预约其他日期");
               }

            }
        }
        return count;
    }
}
